#ifndef _SYS_H_
#define _SYS_H_

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h> 
#include <errno.h>

/**/
#include <fcntl.h>
#include <sys/types.h>

/*socket net*/
#include <sys/types.h>
#include <sys/socket.h>
#include <errno.h>
#include <arpa/inet.h>

/*share*/
#include "app_delay.h"

//#define NDEBUG /*程序调试完后，关掉assert*/
#include <assert.h>




/*dbg*/
#define _dbg  printf
#define myarg(nmb)  _dbg("%s = %d\n", #nmb, nmb)
#define myargx(nmb) _dbg("%s = 0x%x\n", #nmb, nmb)
#define DBGLINE() _dbg("#%s.%d\n", __func__, __LINE__)



int sys_popen(char *p_arg, unsigned short argbuf_len);
void myarray(unsigned char *data, unsigned int len);
void console_lv_set(unsigned char ch);
void console_lv_clear(unsigned char ch);
unsigned int console_lv_get(unsigned char ch);
void set_coredump(void);



#define pr_fmt(fmt) ""fmt
#define console_cond(cond, fmt, args...)\
do {\
    if(cond)\
        printf(pr_fmt(fmt), ##args);\
}while(0)

#define console_log(ch, fmt, args...) console_cond(console_lv_get(ch), fmt, ##args) 
#define console_0_dbg(fmt...) console_log(0,##fmt) 
#define console_1_dbg(fmt...) console_log(1,##fmt) 
#define console_2_dbg(fmt...) console_log(2,##fmt) 
#define console_3_dbg(fmt...) console_log(3,##fmt) 
#define main_dbg	console_1_dbg
#define iic_dbg		console_2_dbg



#define tdbg(fmt, args...)\
do {\
    volatile long t = getNowTime();\
    printf("[%04ld.%03ld] "fmt, t/100, (t%100)*10, ##args);\
}while(0)



/*常用*/
#define MIN(a,b) ( ((a)>=(b))?(b):(a))
#define MAX(a,b) ( ((a)>=(b))?(a):(b))
#define Swap32(value) \
( ((unsigned int)(value) & 0x000000FFU) << 24 | ((unsigned int)(value) & 0x0000FF00U) << 8 | \
((unsigned int)(value) & 0x00FF0000U) >> 8 | ((unsigned int)(value) & 0xFF000000U) >> 24 )

#define Swap16(value) \
( ((unsigned short)(value) & 0x00FFU) << 8 | ((unsigned short)(value) & 0xFF00U) >> 8 )

#define FMT_IP4 "%d.%d.%d.%d"


//获取文件的编译时间
#define BUILD_MONTH_IS_JAN (__DATE__[0] == 'J' && __DATE__[1] == 'a' && __DATE__[2] == 'n')
#define BUILD_MONTH_IS_FEB (__DATE__[0] == 'F')
#define BUILD_MONTH_IS_MAR (__DATE__[0] == 'M' && __DATE__[1] == 'a' && __DATE__[2] == 'r')
#define BUILD_MONTH_IS_APR (__DATE__[0] == 'A' && __DATE__[1] == 'p')
#define BUILD_MONTH_IS_MAY (__DATE__[0] == 'M' && __DATE__[1] == 'a' && __DATE__[2] == 'y')
#define BUILD_MONTH_IS_JUN (__DATE__[0] == 'J' && __DATE__[1] == 'u' && __DATE__[2] == 'n')
#define BUILD_MONTH_IS_JUL (__DATE__[0] == 'J' && __DATE__[1] == 'u' && __DATE__[2] == 'l')
#define BUILD_MONTH_IS_AUG (__DATE__[0] == 'A' && __DATE__[1] == 'u')
#define BUILD_MONTH_IS_SEP (__DATE__[0] == 'S')
#define BUILD_MONTH_IS_OCT (__DATE__[0] == 'O')
#define BUILD_MONTH_IS_NOV (__DATE__[0] == 'N')
#define BUILD_MONTH_IS_DEC (__DATE__[0] == 'D')
#define BUILD_M0 \
((BUILD_MONTH_IS_OCT || BUILD_MONTH_IS_NOV || BUILD_MONTH_IS_DEC) ? '1' : '0')
#define BUILD_M1 \
( \
(BUILD_MONTH_IS_JAN) ? '1' : \
(BUILD_MONTH_IS_FEB) ? '2' : \
(BUILD_MONTH_IS_MAR) ? '3' : \
(BUILD_MONTH_IS_APR) ? '4' : \
(BUILD_MONTH_IS_MAY) ? '5' : \
(BUILD_MONTH_IS_JUN) ? '6' : \
(BUILD_MONTH_IS_JUL) ? '7' : \
(BUILD_MONTH_IS_AUG) ? '8' : \
(BUILD_MONTH_IS_SEP) ? '9' : \
(BUILD_MONTH_IS_OCT) ? '0' : \
(BUILD_MONTH_IS_NOV) ? '1' : \
(BUILD_MONTH_IS_DEC) ? '2' : \
/* error default */ '?')
#define BUILD_Y0 (__DATE__[ 7])
#define BUILD_Y1 (__DATE__[ 8])
#define BUILD_Y2 (__DATE__[ 9])
#define BUILD_Y3 (__DATE__[10])
#define BUILD_D0 ((__DATE__[4] >= '0') ? (__DATE__[4]) : '0')
#define BUILD_D1 (__DATE__[ 5])	


#endif

